package com.learning.algorithm.util;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: daqian_liao
 * @Date: 2018/12/13 20:00
 * @Version 1.0
 */
public class StringUtil {


    public static List<String> mergeAdjacentWord(List<String> texts) {
        List<String> result = new ArrayList<>();
        if (texts != null)
            for (int i = 0; i < texts.size() - 1; i++) {
                result.add(texts.get(i) + texts.get(i + 1));
            }
        return result;
    }

    public static List<String> mergeSingle(List<String> texts) {
        Object[] objects = texts.toArray();
        List<Integer> collect = texts.stream().map(x -> x.length() == 1 ? 1 : 0).collect(Collectors.toList());
        texts.stream().mapToInt(x -> x.length() == 1 ? 1 : 0).sum();
        int[] ints = texts.stream().mapToInt(x -> x.length() == 1 ? 1 : 0).toArray();
        return findSingle(objects, collect);
    }

    public static List<String> findSingle(Object[] texts, List<Integer> singles) {

        List<String> result = new ArrayList<>();

        while (sum(singles) != 0) {
            int[] ints = singles.stream().mapToInt(x -> x).toArray();
            int length = ints.length;
//            int[] cal = new int[length];
            List<Integer> cal = new ArrayList<>();
            cal.add(0);
            for (int i = 1; i < length; i++) {
                int temp = ints[i - 1] + ints[i];
                cal.add(temp == 2 ? 1 : 0);
                if (temp == 2) {
                    result.add(String.valueOf(texts[i - 1]) + texts[i]);
                }
            }
            singles = cal;
        }
        return result;
    }

    private static int sum(List<Integer> ints) {
        return ints.stream().mapToInt(x -> (int) x).sum();

    }

    public static void main(String[] args) {
        List<String> a = new ArrayList<>();
        a.add("mergeSingle");
        a.add("b");
        a.add("cc");
        mergeSingle(a);
    }
}
